package cn.ibizlab.odoo.core.odoo_account.service.impl;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
import java.util.Collection;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import cn.ibizlab.odoo.core.client.model.Iaccount_fiscal_position;
import cn.ibizlab.odoo.core.odoo_account.domain.Account_fiscal_position;
import cn.ibizlab.odoo.core.odoo_account.filter.Account_fiscal_positionSearchContext;
import cn.ibizlab.odoo.core.odoo_account.service.IAccount_fiscal_positionService;
import cn.ibizlab.odoo.util.SearchResult;
import cn.ibizlab.odoo.util.log.IBIZLog;

/**
 * 实体[税科目调整] 服务对象接口实现
 */
@Slf4j
@IBIZLog
@Service
public class Account_fiscal_positionServiceImpl implements IAccount_fiscal_positionService {

    @Autowired
    private cn.ibizlab.odoo.core.client.service.Iaccount_fiscal_positionClientService account_fiscal_positionClientService;

    @Override
    public boolean remove(Integer id) {
        boolean bOk = false ;
        Iaccount_fiscal_position clientModel = account_fiscal_positionClientService.createModel();
        clientModel.setId(id);
        account_fiscal_positionClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public void createBatch(List<Account_fiscal_position> account_fiscal_positions, int batchSize) {

                        //account_fiscal_positionClientService.createBatch(account_fiscal_positions,batchSize) ;

    }

    @Override
    public boolean create(Account_fiscal_position account_fiscal_position) {
        boolean bOk = false ;
        Iaccount_fiscal_position clientModel = this.convert2Model(account_fiscal_position,null);
        account_fiscal_positionClientService.create(clientModel) ;
        this.convert2Domain(clientModel,account_fiscal_position);
        bOk = true ;

        return bOk ;
    }

    @Override
    public void removeBatch(Collection<? extends Serializable> idList, int batchSize) {

        //account_fiscal_positionClientService.removeBatch(account_fiscal_positions) ;

    }

    @Override
    public boolean update(Account_fiscal_position account_fiscal_position) {
        boolean bOk = false ;
        Iaccount_fiscal_position clientModel = this.convert2Model(account_fiscal_position,null);
        account_fiscal_positionClientService.update(clientModel) ;
        this.convert2Domain(clientModel,account_fiscal_position) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public void updateBatch(List<Account_fiscal_position> account_fiscal_positions, int batchSize) {

        //account_fiscal_positionClientService.updateBatch(account_fiscal_positions,batchSize) ;

    }

    @Override
    public Account_fiscal_position get(Integer id) {
        Iaccount_fiscal_position clientModel = account_fiscal_positionClientService.createModel();
        clientModel.setId(id);
        account_fiscal_positionClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public Account_fiscal_position get(Account_fiscal_position account_fiscal_position) {
        Account_fiscal_position retaccount_fiscal_position = this.get(account_fiscal_position.getId()) ;
    	BeanCopier copier=BeanCopier.create(retaccount_fiscal_position.getClass(), account_fiscal_position.getClass(), false);
        copier.copy(retaccount_fiscal_position, account_fiscal_position, null);
        return account_fiscal_position ;
    }

    /**
     * 查询集合 默认查询
     */
    @Override
    public Page<Account_fiscal_position> searchDefault(Account_fiscal_positionSearchContext context) {
        List<Account_fiscal_position> list = new ArrayList<Account_fiscal_position>();
        Page<cn.ibizlab.odoo.core.client.model.Iaccount_fiscal_position> clientModelList = account_fiscal_positionClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iaccount_fiscal_position clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Account_fiscal_position>(list, context.getPageable(), clientModelList.getTotalElements());
    }


    /**
     * 根据公司查询
     */
    @Override
    public Page<Account_fiscal_position> selectByOdoo_company(Integer id){
        Account_fiscal_positionSearchContext context = new Account_fiscal_positionSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Account_fiscal_position> list = new ArrayList<Account_fiscal_position>();
        Page<cn.ibizlab.odoo.core.client.model.Iaccount_fiscal_position> clientModelList = account_fiscal_positionClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iaccount_fiscal_position clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Account_fiscal_position>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据国家/地区群组查询
     */
    @Override
    public Page<Account_fiscal_position> selectByOdoo_country_group(Integer id){
        Account_fiscal_positionSearchContext context = new Account_fiscal_positionSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Account_fiscal_position> list = new ArrayList<Account_fiscal_position>();
        Page<cn.ibizlab.odoo.core.client.model.Iaccount_fiscal_position> clientModelList = account_fiscal_positionClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iaccount_fiscal_position clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Account_fiscal_position>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据国家/地区查询
     */
    @Override
    public Page<Account_fiscal_position> selectByOdoo_country(Integer id){
        Account_fiscal_positionSearchContext context = new Account_fiscal_positionSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Account_fiscal_position> list = new ArrayList<Account_fiscal_position>();
        Page<cn.ibizlab.odoo.core.client.model.Iaccount_fiscal_position> clientModelList = account_fiscal_positionClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iaccount_fiscal_position clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Account_fiscal_position>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Account_fiscal_position> selectByOdoo_create(Integer id){
        Account_fiscal_positionSearchContext context = new Account_fiscal_positionSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Account_fiscal_position> list = new ArrayList<Account_fiscal_position>();
        Page<cn.ibizlab.odoo.core.client.model.Iaccount_fiscal_position> clientModelList = account_fiscal_positionClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iaccount_fiscal_position clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Account_fiscal_position>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Account_fiscal_position> selectByOdoo_write(Integer id){
        Account_fiscal_positionSearchContext context = new Account_fiscal_positionSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Account_fiscal_position> list = new ArrayList<Account_fiscal_position>();
        Page<cn.ibizlab.odoo.core.client.model.Iaccount_fiscal_position> clientModelList = account_fiscal_positionClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iaccount_fiscal_position clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Account_fiscal_position>(list, context.getPageable(), clientModelList.getTotalElements());

    }



    public Iaccount_fiscal_position convert2Model(Account_fiscal_position domain , Iaccount_fiscal_position model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = account_fiscal_positionClientService.createModel() ;
        }

         if(domain.get__last_updateDirtyFlag())
            model.set__last_update(domain.get__last_update());
         if(domain.getNameDirtyFlag())
            model.setName(domain.getName());
         if(domain.getVat_requiredDirtyFlag())
            model.setVat_required(domain.getVat_required());
         if(domain.getAccount_idsDirtyFlag())
            model.setAccount_ids(domain.getAccount_ids());
         if(domain.getStates_countDirtyFlag())
            model.setStates_count(domain.getStates_count());
         if(domain.getSequenceDirtyFlag())
            model.setSequence(domain.getSequence());
         if(domain.getState_idsDirtyFlag())
            model.setState_ids(domain.getState_ids());
         if(domain.getAuto_applyDirtyFlag())
            model.setAuto_apply(domain.getAuto_apply());
         if(domain.getWrite_dateDirtyFlag())
            model.setWrite_date(domain.getWrite_date());
         if(domain.getDisplay_nameDirtyFlag())
            model.setDisplay_name(domain.getDisplay_name());
         if(domain.getIdDirtyFlag())
            model.setId(domain.getId());
         if(domain.getNoteDirtyFlag())
            model.setNote(domain.getNote());
         if(domain.getZip_toDirtyFlag())
            model.setZip_to(domain.getZip_to());
         if(domain.getTax_idsDirtyFlag())
            model.setTax_ids(domain.getTax_ids());
         if(domain.getActiveDirtyFlag())
            model.setActive(domain.getActive());
         if(domain.getCreate_dateDirtyFlag())
            model.setCreate_date(domain.getCreate_date());
         if(domain.getZip_fromDirtyFlag())
            model.setZip_from(domain.getZip_from());
         if(domain.getCreate_uid_textDirtyFlag())
            model.setCreate_uid_text(domain.getCreate_uid_text());
         if(domain.getCountry_group_id_textDirtyFlag())
            model.setCountry_group_id_text(domain.getCountry_group_id_text());
         if(domain.getCompany_id_textDirtyFlag())
            model.setCompany_id_text(domain.getCompany_id_text());
         if(domain.getCountry_id_textDirtyFlag())
            model.setCountry_id_text(domain.getCountry_id_text());
         if(domain.getWrite_uid_textDirtyFlag())
            model.setWrite_uid_text(domain.getWrite_uid_text());
         if(domain.getCompany_idDirtyFlag())
            model.setCompany_id(domain.getCompany_id());
         if(domain.getCountry_idDirtyFlag())
            model.setCountry_id(domain.getCountry_id());
         if(domain.getCreate_uidDirtyFlag())
            model.setCreate_uid(domain.getCreate_uid());
         if(domain.getWrite_uidDirtyFlag())
            model.setWrite_uid(domain.getWrite_uid());
         if(domain.getCountry_group_idDirtyFlag())
            model.setCountry_group_id(domain.getCountry_group_id());
        return model ;
    }

    public Account_fiscal_position convert2Domain( Iaccount_fiscal_position model ,Account_fiscal_position domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new Account_fiscal_position();
        }

        if(model.get__last_updateDirtyFlag())
            domain.set__last_update(model.get__last_update());
        if(model.getNameDirtyFlag())
            domain.setName(model.getName());
        if(model.getVat_requiredDirtyFlag())
            domain.setVat_required(model.getVat_required());
        if(model.getAccount_idsDirtyFlag())
            domain.setAccount_ids(model.getAccount_ids());
        if(model.getStates_countDirtyFlag())
            domain.setStates_count(model.getStates_count());
        if(model.getSequenceDirtyFlag())
            domain.setSequence(model.getSequence());
        if(model.getState_idsDirtyFlag())
            domain.setState_ids(model.getState_ids());
        if(model.getAuto_applyDirtyFlag())
            domain.setAuto_apply(model.getAuto_apply());
        if(model.getWrite_dateDirtyFlag())
            domain.setWrite_date(model.getWrite_date());
        if(model.getDisplay_nameDirtyFlag())
            domain.setDisplay_name(model.getDisplay_name());
        if(model.getIdDirtyFlag())
            domain.setId(model.getId());
        if(model.getNoteDirtyFlag())
            domain.setNote(model.getNote());
        if(model.getZip_toDirtyFlag())
            domain.setZip_to(model.getZip_to());
        if(model.getTax_idsDirtyFlag())
            domain.setTax_ids(model.getTax_ids());
        if(model.getActiveDirtyFlag())
            domain.setActive(model.getActive());
        if(model.getCreate_dateDirtyFlag())
            domain.setCreate_date(model.getCreate_date());
        if(model.getZip_fromDirtyFlag())
            domain.setZip_from(model.getZip_from());
        if(model.getCreate_uid_textDirtyFlag())
            domain.setCreate_uid_text(model.getCreate_uid_text());
        if(model.getCountry_group_id_textDirtyFlag())
            domain.setCountry_group_id_text(model.getCountry_group_id_text());
        if(model.getCompany_id_textDirtyFlag())
            domain.setCompany_id_text(model.getCompany_id_text());
        if(model.getCountry_id_textDirtyFlag())
            domain.setCountry_id_text(model.getCountry_id_text());
        if(model.getWrite_uid_textDirtyFlag())
            domain.setWrite_uid_text(model.getWrite_uid_text());
        if(model.getCompany_idDirtyFlag())
            domain.setCompany_id(model.getCompany_id());
        if(model.getCountry_idDirtyFlag())
            domain.setCountry_id(model.getCountry_id());
        if(model.getCreate_uidDirtyFlag())
            domain.setCreate_uid(model.getCreate_uid());
        if(model.getWrite_uidDirtyFlag())
            domain.setWrite_uid(model.getWrite_uid());
        if(model.getCountry_group_idDirtyFlag())
            domain.setCountry_group_id(model.getCountry_group_id());
        return domain ;
    }


}
